from PIL import Image
from io import BytesIO
import base64
import json
from flask import Flask, request
from network import getnetwork
import numpy as np

app = Flask(__name__)
model = getnetwork()
class_names = ["上衣", "裤子", "套头衫", "连衣裙", "外套", "凉鞋", "衬衫", "运动鞋", "包", "短靴"]


@app.route('/', methods=["POST"])
def parsepic():
    reqDic = json.loads(request.data.decode())
    cname = reqDic["className"]
    img = base64.b64decode(reqDic["picUrl"].split(",")[1])
    img = Image.open(BytesIO(img))
    img = img.convert("1")
    img = img.resize((28, 28), Image.ANTIALIAS)
    img = (np.expand_dims(img, 0))
    predictArr = model.predict(img)
    score = predictArr[0][class_names.index(cname)] / np.sum(predictArr) * 100
    return str(score)


if __name__ == "__main__":
    app.run(host='127.0.0.1', port=3001)
